<?php

namespace app\api\controller\v2;

use app\common\controller\Api;
use app\common\model\ContractError;
use app\common\model\Message as ThisModel;
use think\Db;

/**
 * 消息接口
 */
class Message extends Api
{
    protected $noNeedRight = '*';

    /**
     * 获取我的消息列表
     */
    public function lists()
    {
        if ($this->request->isPost()) {

            if ($this->request->has('limit')) {
                $page_size = $this->request->param('limit/d');
            } else {
                $page_size = $this->request->param('page_size/d');
            }
            if ($page_size <= 0) {
                $page_size = 10;
            }

            $type = $this->request->param('type/d',0);
            $where = [
                'user_id' => $this->auth->id
            ];

            if ($type) {
                $where['type'] = $type;
            }

            $Data = ThisModel::where($where)
                ->order('createtime desc')
                ->paginate($page_size)
                ->each(function ($item) {
                    $item['status_text'] = ThisModel::getStatusList(1, $item['status']);
                    $item['createtime'] = date('Y-m-d H:i', $item['createtime']);
                    return $item;
                });
            $this->success('查询成功', $Data);
        } else {
            $this->error('请求参数有误');
        }
    }

    /**
     * 获取我的消息详情
     */
    public function detail()
    {
        if ($this->request->isPost()) {

            $id = $this->request->post('id/d');
            $user_id = $this->auth->id;

            if (!$id) {
                $this->error('参数错误');
            }
            $where = [
                'user_id' => $user_id,
                'id' => $id
            ];
            $item = ThisModel::where($where)->find();
            if (!$item) {
                $this->error('获取失败');
            }
            $item['status_text'] = ThisModel::getStatusList(1, $item['status']);
            ThisModel::where(['id' => $item['id']])->update(['status' => ThisModel::STATUS_NORMAL]);
            $other_data = [];
            if ($item['type'] == ThisModel::TYPE_THREE) {
                $error_info = ContractError::where(['id' => $item['other_id']])->find();
                $contract_info = \app\common\model\Contract::where(['id' => $error_info['contract_id']])->find();
                $error_info_type_text = ContractError::getTypeList(1, $error_info['type']);
                $other_data['build_address'] = $contract_info['build_address'];
                $other_data['room_name'] = $contract_info['room_name'];
                $other_data['homeowner_name'] = $contract_info['homeowner_name'];
                $other_data['homeowner_phone'] = $contract_info['homeowner_phone'];
                $other_data['error_type_text'] = $error_info_type_text;
                $other_data['error_content'] = $error_info['title'];
                $other_data['error_photo'] = $error_info['photo'];
            }
            $item['other_data'] = $other_data;
            $this->success('查询成功', $item);
        } else {
            $this->error('请求参数有误');
        }
    }

    /**
     * 获取我的消息列表-用户端
     */
    public function new_lists()
    {
        if ($this->request->isPost()) {

            $where = [
                'user_id' => $this->auth->id,
                'type' => ['NEQ', ThisModel::TYPE_FORE],
            ];
            $Data = ThisModel::where($where)
                ->group('type')
                ->order('status asc,id desc')
                ->select();
            $zixun = [];
            $dongzhang = [];
            $gongdan = [
                'id' => 0,
                'title' => '暂无内容',
                'status' => 1,
                'create_time' => date('Y-m-d H:i', time()),
            ];
            $zhangdan = [
                'id' => 0,
                'title' => '暂无内容',
                'status' => 1,
                'create_time' => date('Y-m-d H:i', time()),
            ];
            $notice = [
                'id' => 0,
                'title' => '暂无内容',
                'status' => 1,
                'create_time' => date('Y-m-d H:i', time()),
            ];
            foreach ($Data as $v) {
                if ($v['type'] == ThisModel::TYPE_THREE) {
                    $gongdan = [
                        'id' => $v['id'],
                        'title' => $v['title'],
                        'status' => $v['status'],
                        'create_time' => date('Y-m-d H:i', $v['createtime']),
                    ];
                }
                if ($v['type'] == ThisModel::TYPE_ONE) {
                    $zhangdan = [
                        'id' => $v['id'],
                        'title' => $v['title'],
                        'status' => $v['status'],
                        'create_time' => date('Y-m-d H:i', $v['createtime']),
                    ];
                }

                if ($v['type'] == ThisModel::TYPE_FIVE) {
                    $notice = [
                        'id' => $v['id'],
                        'title' => $v['title'],
                        'status' => $v['status'],
                        'create_time' => date('Y-m-d H:i', $v['createtime']),
                    ];
                }
            }
            if ($zhangdan['status'] == 1) {
                foreach ($Data as $v) {
                    if ($v['type'] == ThisModel::TYPE_TWO && $v['status'] == ThisModel::STATUS_DEFAULT) {
                        $zhangdan = [
                            'id' => $v['id'],
                            'title' => $v['title'],
                            'status' => $v['status'],
                            'create_time' => date('Y-m-d H:i', $v['createtime']),
                        ];
                    }
                }
            }
            $re_data = [
                'zixun' => $zixun,
                'dongzhang' => $dongzhang,
                'gongdan' => $gongdan,
                'zhangdan' => $zhangdan,
                'notice' => $notice,
            ];
            $this->success('查询成功', $re_data);
        } else {
            $this->error('请求参数有误');
        }
    }

    /**
     * 获取我的消息列表-房东端
     */
    public function fang_lists()
    {
        if ($this->request->isPost()) {

            $user_id = $this->auth->id;
            if ($this->request->has('limit')) {
                $page_size = $this->request->param('limit/d');
            } else {
                $page_size = $this->request->param('page_size/d');
            }
            if ($page_size <= 0) {
                $page_size = 10;
            }
            $where = [
                'user_id' => $user_id,
                'type' => ['in', [ThisModel::TYPE_THREE, ThisModel::TYPE_FORE]],
            ];

            $Data = ThisModel::where($where)
                ->order('createtime desc')
                ->paginate($page_size)
                ->each(function ($item) {
                    $item['status_text'] = ThisModel::getStatusList(1, $item['status']);
                    $item['createtime'] = date('Y-m-d H:i', $item['createtime']);
                    return $item;
                });
            $this->success('查询成功', $Data);
        } else {
            $this->error('请求参数有误');
        }
    }
    public function getUserMessageNum()
    {
        $where = [
            'user_id' => $this->auth->id,
            'status' => 0,
            'type' => 5,
        ];
        $count =  ThisModel::where($where)->count();
        $this->success("未读数量",$count);
    }
}
